#@HplusLayout()
#define content()
<div style="padding:15px">
	<form class="form-horizontal"> 
		<input type="hidden" name="path" id="path"/>
		<div class="form-group">
			<label class="col-xs-2 control-label" style="text-align: right;padding-top: 7px;">文件路径：</label>
			<div class="col-xs-9">
				<div class="input-group">
			  	<input type="file" class="form-control" id="files" readonly="readonly"/>
				</div>
				<div id="progress"></div>
			</div>
		</div>
		<div class="form-group">
    	<div class="col-xs-9 col-xs-offset-2">
      	<button type="button" onclick="submitPath();" class="btn btn-success">确定</button>
      </div>
    </div>
	</form>
</div>
#end
#define jsFunction()
<script>
 	$(function(){
 		$("#files").on("change",function(){
			var upload_token = "#(upload_token)";
			if(upload_token==""){
				layer.alert("无法上传",{icon:2});
				return;
			}
			layer.load(1, {shade: [0.5,'#fff']});
			var formData = new FormData();
			formData.append("files",this.files[0]);
			formData.append("upload_token",upload_token);
			$.ajax({
		  	url:"#(path)/portal/upload/addFilesByForm",
		    type: "POST",
		    cache: false,
		    data: formData,
		    processData: false,
		    contentType: false,
		    dataType:"json",
		    xhr: function(){
        	myXhr = $.ajaxSettings.xhr();  
          if(myXhr.upload){
            myXhr.upload.addEventListener("progress",function(e){
            	$("#progress").html(parseInt(e.loaded/e.total*100)+"%");
            }, false);   
          }
          return myXhr;
       	},  
		    success : function(data) {
		    	layer.closeAll();
		    	if(data.result=="success"){
		    		$("#path").val(data.url);
		    		layer.msg("上传成功");
		    	}else {
						layer.alert(data.message,{icon:2});
		    	}
		    },error:function(){
					layer.alert("与服务器通讯失败",{icon:2});
		    }
		  });
 		})	
 	});
 	
 	function submitPath(){
 		var path = $("#path").val();
 		if(path==""){
 			layer.alert("请先选择文件！");
 			return;
 		}
 		var input = $("##(input??"NULL")", window.parent.document);
 		var upload_token = $("input[name='upload_token']", window.parent.document);
 		if(input.size()>0){
 			input.val(path);
 			upload_token.val("#(upload_token)");
 		}
 		window.parent.layer.closeAll();
 	}
</script>
#end